package com.example.myapplication.urbansubway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.DroutedetailsBean;
import com.example.myapplication.ShuJu.RouteclassificationBean;
import com.example.myapplication.apter.RouteclassificationApter;
import com.example.myapplication.apter.SubwayroutesApter;

public class RoutedetailsActivity extends AppCompatActivity {

    private String routedetailsid;
    private String TAG = "TAG";
    private TextView endTime;
    private TextView stationsNumber;
    private TextView km;
    private TextView end;
    private TextView startTime;
    private TextView runStationsName;
    private TextView first;
    private TextView name;
    private ImageView img;
    private RecyclerView re;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_routedetails);
        initview();
        but_bloak();
        sendroutedetails();

    }

    private void subwayroutes(DroutedetailsBean.DataDTO data, String name) {
        Log.e(TAG, name);
        re.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        re.setAdapter(new SubwayroutesApter(this, data.getMetroStepList(), name));
    }

    private void sendroutedetails() {
        OKHttpTool.doNetRequest("prod-api/api/metro/line/" + routedetailsid,
                OKHttpTool.GET,
                DroutedetailsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(RoutedetailsActivity.this, new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                DroutedetailsBean droutedetailsBean = (DroutedetailsBean) obj;
                                if (droutedetailsBean.getCode() == 200) {
                                    DroutedetailsBean.DataDTO data = droutedetailsBean.getData();
                                    if (data != null) {
                                        //地铁路线
                                        subwayroutes(data, data.getRunStationsName());
                                        name.setText(data.getName());
                                        first.setText("起点: " + data.getFirst());
                                        runStationsName.setText(data.getRunStationsName());
                                        startTime.setText("起 " + data.getStartTime());
                                        endTime.setText("末 " + data.getEndTime());
                                        end.setText("终点: " + data.getEnd());
                                        stationsNumber.setText("/" + String.valueOf(data.getStationsNumber()) + "站");
                                        km.setText(String.valueOf(data.getKm()) + "km");
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RoutedetailsActivity.this.finish();
            }
        });
    }


    private void initview() {
        Intent intent = getIntent();
        //路线id
        routedetailsid = intent.getStringExtra(Commom.ROUTEDETAILSID);
        img = findViewById(R.id.img);
        name = findViewById(R.id.name);
        first = findViewById(R.id.first);
        runStationsName = findViewById(R.id.runStationsName);
        startTime = findViewById(R.id.startTime);
        end = findViewById(R.id.end);
        km = findViewById(R.id.km);
        stationsNumber = findViewById(R.id.stationsNumber);
        endTime = findViewById(R.id.endTime);
        re = findViewById(R.id.re);
    }

}